<template>
  <basic-container :btnFlag=true>
    <div class="card-border">
      <card-header :header-data="headerData"></card-header>
      <div class="card-body">
        <avue-crud :data="subjectOpinionListData"
                   :option="tableoption"
                   :page="page"
                   :table-loading="tableLoading"
                   @size-change="sizeChange"
                   @current-change="currentChange"
                   @refresh-change="refreshChange"
                   @search-change="searchChange"
                   @search-reset="refreshChange"
                   class="avuecrud">
          <template slot="menuRight">
            <el-button type="primary" size="mini" @click="addProgram()">编制责任分解方案</el-button>
          </template>
          <template slot-scope="scope" slot="menu">
            <div class="menu-title" @click.stop="programOpinion(scope.row,scope.index)"
                 v-if="scope.row.state!='2'&&scope.row.state!='1'&&scope.row.deptOperable!='0'">
              <i class="iconfont iconview icon-primary"></i>
              <p class="menu-title-text">征求意见</p>
            </div>
            <div class="menu-title" @click.stop="collectionListProgram(scope.row,scope.index)"
                 v-if="scope.row.state!='2'&&scope.row.state=='1'">
              <i class="iconfont iconview icon-primary"></i>
              <p class="menu-title-text">查看反馈</p>
            </div>
            <div class="menu-title" @click.stop="subjectOpinionDeletee(scope.row.id)"
                 v-if="scope.row.state!='2'&&scope.row.deptOperable!='0'">
              <i class="iconfont iconalter icon-warning"></i>
              <p class="menu-title-text">修改方案</p>
            </div>
            <div class="menu-title" @click.stop="releaseProgram(scope.row,scope.index)"
                 v-if="scope.row.state!='2'&&scope.row.deptOperable!='0'">
              <i class="iconfont iconview icon-primary"></i>
              <p class="menu-title-text">下达方案</p>
            </div>
          </template>
        </avue-crud>
      </div>
      <el-dialog title="下达方案" :visible.sync="releaseProgramVisible" width="70%" @close="close">
        <el-form
          label-width="190px"
          :model="releaseForm"
          style="padding-right: 30px;"
          ref="releaseForm"
          :rules="releaseFormRules"
        >
          <el-row>
            <el-col :span="15">
              <el-form-item class="form-item" label="文件名称 :" prop="name">
                <el-input v-model="releaseForm.name" placeholder="请输入文件名称"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="15">
              <el-form-item class="form-item" label="下达单位 :" prop="releaseCompany">
                <el-input v-model="releaseForm.releaseCompany" placeholder="请输入单位名称" disabled></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="15">
              <el-form-item class="form-item" label="主送单位 :" prop="mainCompany">
                <el-input v-model="releaseForm.mainCompany" placeholder="请输入单位名称" disabled></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="16">
              <el-form-item label="抄送单位 :" prop="copyCompany" class="form-item">
                <span style="font-size: 16px">{{releaseForm.copyCompany}}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-button size="small" type="primary" icon="el-icon-plus" style="margin-left: 10px;"
                         @click="deptVisible=true">选择抄送单位
              </el-button>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="20">
              <el-form-item class="form-item"
                            label="年度监测资料报送时间 :"
                            prop="yearStartTime">
                <el-date-picker
                  v-model="releaseForm.yearStartTime"
                  type="date"
                  placeholder="选择日期"
                  value-format="MM-dd"
                  format="MM-dd"
                  :picker-options="expireTimeOption">
                </el-date-picker>
                <span style="margin-left: 5px;margin-right: 5px">至</span>
                <el-date-picker
                  v-model="releaseForm.yearEndTime"
                  type="date"
                  placeholder="选择日期"
                  value-format="MM-dd"
                  format="MM-dd"
                  :picker-options="expireTimeOption">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="20">
              <el-form-item class="form-item"
                            label="中期评估资料报送时间 :"
                            prop="midStartTime">
                <el-date-picker
                  v-model="releaseForm.midStartTime"
                  type="date"
                  placeholder="选择日期"
                  value-format="yyyy-MM-dd"
                  format="yyyy-MM-dd"
                  :picker-options="expireTimeOption">
                </el-date-picker>
                <span style="margin-left: 5px;margin-right: 5px">至</span>
                <el-date-picker
                  v-model="releaseForm.midEndTime"
                  type="date"
                  placeholder="选择日期"
                  value-format="yyyy-MM-dd"
                  format="yyyy-MM-dd"
                  :picker-options="expireTimeOption">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="22">
              <el-form-item class="form-item" label="附件 :" prop="content">
                <el-upload
                  class="upload-demo"
                  ref="uploadrelated"
                  action="string"
                  :http-request="submitUploadMixin"
                  :on-preview="handlePreviewMixin"
                  :on-remove="handleRemoveMixin"
                  :file-list="fileListMixin"
                  :auto-upload="true"
                  :on-change="fileChangeMixin">
                  <el-button slot="trigger" size="small" type="primary">点击上传</el-button>
                </el-upload>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="confirmReleaseForm()">确 定</el-button>
          <el-button @click="releaseProgramVisible=false">取 消</el-button>
        </div>
      </el-dialog>
      <!--抄送单位部门选择-->
      <dept-check :visible="deptVisible" @getCheckInfo="getSelectInfo"
                  @cancel="cancelDept"></dept-check>
    </div>
  </basic-container>
</template>

<script>
  import {getDecomposeDraftList, releaseProgram, getItemUnits} from "@/api/pim/pim";
  import relatedfile from "@/mixins/relatedfile";

  export default {
    name: "modelList",
    mixins: [relatedfile],
    created() {
      this.getResultList(this.page);
      window.subjectOpinionDeletee = this.subjectOpinionDeletee;
    },
    data() {
      return {
        releaseFormRules: {
          copyCompany: [
            {required: true, message: "请选择抄送单位", trigger: "change"}
          ],
          yearStartTime: [
            {required: true, message: "请选择年度监测资料报送时间", trigger: "change"}
          ],
          midStartTime: [
            {required: true, message: "请选择中期评估资料报送时间", trigger: "change"}
          ],
        },
        deptVisible: false,
        headerData: {
          title: '责任分解'
        },
        subjectOpinionListData: [],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条
        },
        tableLoading: false,
        tableoption: {
          page: true,
          menuWidth: 300,
          addBtn: false,
          editBtn: false,
          addRowBtn: false,
          indexLabel: "序号",
          index: false,
  indexWidth:70,
          cellBtn: false,
          delBtn: false,
          columnBtn: false,
          refreshBtn: false,
          filterBtn: false,
          searchBtn: false,
          keyId: "id",
          column: [
            {
              label: "规划名称",
              prop: "planningTitle",
              search: true,
              cell: true,
              formatter: function (row, value, label, column) {
                return '<a style="cursor: pointer; " onclick="subjectOpinionDeletee(\'' + row.id + '\',\'' + 1 + '\')">' + value + '</a>';
              },
              // formatter: function (row, value, label, column) {
              //   if(row.state =='2'){
              //     return '<a style="cursor: pointer; text-decoration:underline !important" onclick="subjectOpinionDeletee(\'' + row.id + '\',\'' + 1 + '\')">' + value + '分解方案' + '</a>';
              //   }else{
              //     return '<a style="cursor: pointer; text-decoration:underline !important" onclick="subjectOpinionDeletee(\'' + row.id + '\',\'' + 1 + '\')">' + value + '分解方案征求意见稿' + '</a>';
              //   }
              // },
            },
            {
              label: "状态",
              prop: "state",
              cell: true,
              width: 100,
              dicData: [
                {
                  label: '已编制',
                  value: '0'
                }, {
                  label: '征求意见中',
                  value: '1'
                }, {
                  label: '已下达',
                  value: '2'
                },
              ]
            },
          ]
        },
        releaseProgramVisible: false,
        releaseForm: {
          mainCompany: '',
          name: '',
          releaseCompany: '',
          copyCompany: '',
          copyId: '',
          yearStartTime: null,
          yearEndTime: null,
          midStartTime: null,
          midEndTime: null,
          fileIds: [],

        },
        programIdtemp: null,
        expireTimeOption: {
          disabledDate(date) { //disabledDate 文档上：设置禁用状态，参数为当前日期，要求返回 Boolean
            return date.getTime() <= Date.now();
          }
        },
        planningTitle:''

      }
    },

    methods: {
      cancelDept(deptIds, deptNames) {
        this.deptVisible = false;
      },
      getSelectInfo(copyId, copyCompany) {
        this.releaseForm.copyCompany = copyCompany;
        this.releaseForm.copyId = copyId.split(",");
        this.deptVisible = false;
      },
      close() {
        let releaseForm = {
          mainCompany: '',
          name: '',
          releaseCompany: '',
          copyCompany: '',
          yearStartTime: null,
          yearEndTime: null,
          midStartTime: null,
          midEndTime: null,
          fileIds: [],

        }
      },
      subjectOpinionDeletee(id, watch) {
        sessionStorage.setItem('modifyProgram', id);
        let src = '/modifyProgram/detail';
        this.$router.push({
          path: src,
          query: {
            watch: watch,
            draftId: id,
          }
        })
      },
      /**
       * 刷新回调
       */
      refreshChange () {
        this.planningTitle= '';
        this.getResultList(this.page);
      },
      //搜索回调
      searchChange (params) {
        this.page.currentPage = 1
        this.getResultList(this.page, this.dealSearchParam(params));
      },
      // 处理搜索过滤
      dealSearchParam (params) {
        if (params.planningTitle) {
          params.planningTitle = params.planningTitle;
        }
        this.planningTitle= params.planningTitle;
        return params;
      },

      currentChange(currentPage) {
        var page = this.page;
        page.currentPage = currentPage;
        var params={planningTitle: this.planningTitle};
        this.getResultList(page, params);
      },
      sizeChange(pageSize) {
        var page = this.page;
        page.pageSize = pageSize;
        var params= {planningTitle: this.planningTitle};
        this.getResultList(page, params);
      },
      getResultList(page, params) {
        getDecomposeDraftList(Object.assign({
          current: page.currentPage,
          size: page.pageSize
        },params)).then(response => {
          this.subjectOpinionListData = response.data.data.records;
          this.page.total = response.data.data.total;
        })
      },
      //责任分解
      addProgram(row) {
        let src = '/addProgram/detail';
        this.$router.push({
          path: src,
        })
      },
      //征求意见
      programOpinion(row) {
        let src = `/programOpinion/detail/${row.id}`;
        this.$router.push({
          path: src,
          query: {
            title: row.planningTitle,
            orgName: row.orgName,
          }
        })
      },
      //查看反馈
      collectionListProgram(row) {
        let src = `/collectionListProgram/list/${row.id}`;
        this.$router.push({
          path: src,
          query: {
            type: '7',
            achievementTitle: row.planningTitle,
          }
        })
      },
      releaseProgram(row) {
        this.releaseProgramVisible = true;
        this.programIdtemp = row.id;
        let rel = this.releaseForm;
        rel.name = row.orgName + '关于下达《' + row.planningTitle + '责任分解方案》的通知';
        rel.releaseCompany = row.orgName;
        getItemUnits(row.id).then(response => {
          if (response.data.code == '0') {
            rel.mainCompany = response.data.data;
          }
        })
      },
      confirmReleaseForm() {
        if (this.releaseForm.yearStartTime == null || this.releaseForm.yearEndTime == null) {
          this.$notify({
            title: "提醒",
            type: "warning",
            message: "请填写年度监测资料报送时间",
            duration: 2000
          });
          return;
        }

        if (this.releaseForm.midStartTime == null || this.releaseForm.midEndTime == null) {
          this.$notify({
            title: "提醒",
            type: "warning",
            message: "请填写中期评估资料报送时间",
            duration: 2000
          });
          return;
        }

        //将多个相关材料的附件id拼成逗号隔开的字符串放在 this.projectForm.fileId 中
        var arr = [];
        for (var item in this.fileListMixin) {
          arr.push(this.fileListMixin[item].url);
        }
        this.releaseForm.fileIds = arr;


        let data = {
          draftId: this.programIdtemp,
          deptIds: this.releaseForm.copyId,
          yearStartTime: this.releaseForm.yearStartTime,
          yearEndTime: this.releaseForm.yearEndTime,
          midStartTime: this.releaseForm.midStartTime,
          midEndTime: this.releaseForm.midEndTime,
          fileIds: this.releaseForm.fileIds,
        };
        this.$refs.releaseForm.validate(valid => {
          if (valid) {
            this.$confirm("确定下达方案吗?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }).then(() => {
              if (this.programIdtemp != null) {
                releaseProgram(data).then(response => {
                  this.$router.go(0)
                })
              } else {
                this.$notify({
                  title: '提示',
                  message: '方案id为空',
                  type: 'warning',
                  duration: 2000
                });
              }
            })
          }
        })

      },

    }

  }
</script>

<style lang="scss" scoped>
  .form-item {
    color: black;
    font-weight: 600;
  }
</style>
